//
//  MPTLiveCountDownView.m
//  MPTLive
//
//  Created by caixun on 15/8/20.
//  Copyright (c) 2015年 Jeakin. All rights reserved.
//

#import "MPTLiveCountDownView.h"

#define COUNTDOWN_LABEL_FONT_SCALE_FACTOR 0.3

@interface MPTLiveCountDownView ()

@property (nonatomic) NSTimer* timer;
@property (nonatomic) UILabel* countdownLabel;
@property (nonatomic) int currentCountdownValue;

@end

@implementation MPTLiveCountDownView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self updateAppearance];
    }
    return self;
}

- (void)updateAppearance
{
    [[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.opaque = NO;
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:self.backgroundAlpha];
    
    float fontSize = self.bounds.size.width * COUNTDOWN_LABEL_FONT_SCALE_FACTOR;
    
    self.countdownLabel = [[UILabel alloc] init];
    [self.countdownLabel setFont:[UIFont fontWithName:self.fontName size:fontSize]];
    [self.countdownLabel setTextColor:self.countdownColor];
    self.countdownLabel.textAlignment = NSTextAlignmentCenter;
    
    self.countdownLabel.opaque = YES;
    self.countdownLabel.alpha = 1.0;
    [self addSubview: self.countdownLabel];
    
    self.countdownLabel.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width + 400, self.frame.size.height);
    [self.countdownLabel setCenter:self.center];
    
    self.finishImgv.opaque = YES;
    self.finishImgv.alpha = 1.0;
    [self addSubview: self.finishImgv];
    self.finishImgv.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 100, 100);
    [self.finishImgv setCenter:self.center];
    [self.finishImgv setHidden:YES];
}


#pragma mark - start/stopping

- (void)start
{
    [self stop];
    self.currentCountdownValue = self.countdownFrom;
    
    self.countdownLabel.text = [NSString stringWithFormat:@"%d", self.countdownFrom];
    [self animate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(animate)
                                                userInfo:nil
                                                 repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stop
{
    if (self.timer && [self.timer isValid])
    {
        [self.timer invalidate];
    }
}

#pragma mark - animation stuff

- (void)animate
{
    [UIView animateWithDuration:0.9 animations:^
    {
        if(self.currentCountdownValue == 0)
        {
            CGAffineTransform transform = CGAffineTransformMakeScale(2.5, 2.5);
            self.finishImgv.transform = transform;
            self.finishImgv.alpha = 0;
        }
        else
        {
            CGAffineTransform transform = CGAffineTransformMakeScale(2.5, 2.5);
            self.countdownLabel.transform = transform;
            self.countdownLabel.alpha = 0;
        }
    }
    completion:^(BOOL finished)
    {
        if (finished)
        {
            if (self.currentCountdownValue == 0)
            {
                [self stop];
                
                [self.finishImgv setHidden:YES];
                [self.countdownLabel setHidden:NO];
                
                if (self.delegate)
                {
                    [self.delegate countdownFinished:self];
                    [self removeFromSuperview];
                }
            }
            else
            {
                if(self.currentCountdownValue == 1)
                {
                    if (self.delegate)
                    {
                        [self.delegate countdownValueToOne:self];
                    }
                }
                self.countdownLabel.transform = CGAffineTransformIdentity;
                self.countdownLabel.alpha = 1.0;
                
                self.currentCountdownValue--;
                if (self.currentCountdownValue == 0)
                {
                    self.countdownLabel.text = self.finishText;
                    
                    [self.finishImgv setHidden:NO];
                    [self.countdownLabel setHidden:YES];
                }
                else
                {
                    self.countdownLabel.text = [NSString stringWithFormat:@"%d", self.currentCountdownValue ];
                }
            }
        }
    }];
}

#pragma mark - custom getters
- (NSString*)finishText
{
    if (!_finishText)
    {
        _finishText = @"Go";
    }
    
    return _finishText;
}

- (float)backgroundAlpha
{
    if (_backgroundAlpha == 0)
    {
        _backgroundAlpha = 0.3;
    }
    
    return _backgroundAlpha;
}

- (int)countdownFrom
{
    if (_countdownFrom == 0)
    {
        _countdownFrom = kDefaultCountdownFrom;
    }
    
    return _countdownFrom;
}

- (UIColor *)countdownColor
{
    if (!_countdownColor)
    {
        _countdownColor = [UIColor blackColor];
    }
    
    return _countdownColor;
}

- (NSString *)fontName
{
    if (!_fontName)
    {
        _fontName = @"HelveticaNeue-Medium";
    }
    
    return _fontName;
}

@end
